package my.service;

import java.util.List;

import my.db.DbManager;
import my.model.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserService {
	public static Log logger = LogFactory.getLog(UserService.class);
	
	private static final String CACHE_NAME = "users";

	public static User findUser(String name) {
		String sql = "select name as username, pwd as password from users where name=?";

		User user = DbManager.cachedGet(CACHE_NAME, name, User.class, sql, name);
		return user;
	}

	public static List<User> topUsers(int top) {
		String sql = "select name as username, pwd as password from users limit ?";

		List<User> userList = DbManager.cachedList(CACHE_NAME, "topUserList", 
				User.class, sql, top); 
		
		return userList;
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();

		User u = findUser("rushmore");
		System.out.println(u.getUsername());
		List<User> userList = DbManager.paging(User.class,
				"select name as username, pwd as password from users", 1, 3);
		for(User user : userList){
			System.out.println(user.getUsername());
		}
		long end = System.currentTimeMillis();

		System.out.println(end - start);
	}

}
